//
//  ViewController.m
//  18-通讯录sqlite
//
//  Created by dllo on 16/8/8.
//  Copyright © 2016年 LST. All rights reserved.
//

#import "ViewController.h"
#import "AddViewController.h"
#import "DetailViewController.h"
#import "AddModel.h"
#import "DataBaseManager.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
#define COLORAUTO [UIColor colorWithRed:(arc4random() % 256 / 255.0) green:(arc4random() % 256 / 255.0) blue:(arc4random() % 256 / 255.0) alpha:1.0]
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource, AddViewControllerDelegate>
@property (nonatomic, retain)
UITableView *tableView;
/** modelArr */
@property (nonatomic, retain) NSMutableArray *arrModel;
@property (nonatomic, strong) DataBaseManager *dataBaseManager;
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.arrModel = [NSMutableArray array];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"联系人列表";
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(clickRightItemAdd:)];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    [self creatTableView];
    _dataBaseManager = [DataBaseManager shareManager];
   [self.arrModel addObjectsFromArray:[_dataBaseManager selectAllPeople]];
}
- (void)creatTableView {
    self.tableView = [[UITableView alloc]
                      initWithFrame:CGRectMake(0,0, WIDTH, HEIGHT)
                      style:2];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}
- (void)clickRightItemAdd:(UIButton *)btn{
    AddViewController *addVC = [AddViewController new];
    addVC.delegate = self;
    [self.navigationController pushViewController:addVC animated:YES];
}
- (void)addModel:(AddModel *)model{
    AddModel *modelOfMainVC = [[AddModel alloc] init];
    modelOfMainVC = model;
    [self.arrModel addObject:modelOfMainVC];
    [self.tableView reloadData];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _arrModel.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:3 reuseIdentifier:@"cell"];
    AddModel *model  = self.arrModel[indexPath.row];
    cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.jpg", model.photo]];
    cell.textLabel.text = model.name;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DetailViewController *detail = [[DetailViewController alloc] init];
    //    detail.arrModels = self.arrModel;
    [self.navigationController pushViewController:detail animated:YES];
}
//- (void)addStudentModel:(AddModel *)model{
//    AddModel *modelResult = [[AddModel alloc] init];
//    modelResult = model;
//    [self.arrModel addObject:modelResult];
//    [self.tableView reloadData];
//}

@end
